package com.czkevin.cif.commands.main;

import com.czkevin.cif.*;
import com.czkevin.cif.commands.CIFMainCommand;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.List;
import java.util.stream.Collectors;

public class GiveCommand implements CIFSubCommand {
    @Override
    public String getCommandName() {
        return "give";
    }

    @Override
    public String getHelpParameters() {
        return "<CIFType>";
    }

    @Override
    public String getHelpText() {
        return "Give CIFItem to you. ";
    }

    @Override
    public void execute(CommandSender sender, String[] args) {
        if (!Player.class.isAssignableFrom(sender.getClass())) return;
        if (!sender.hasPermission(CIFMainCommand.PERMISSION)) return;

        if (args.length == 0) {
            sender.sendMessage("\u00a7cNo name given! ");
            return;
        }

        CIFItem type = ItemRegister.get(args[0]);
        if (type == null) {
            sender.sendMessage("\u00a7cItem type not found! ");
            return;
        }

        Player player = (Player) sender;

        CIFItemInstance instance = new CIFItemInstance(type);
        ItemStack i = CustomItemFramework.makeCIFItem(instance);

        player.getWorld().dropItem(player.getLocation(), i);

        player.sendMessage("\u00a7aItem spawned! ");
    }

    @Override
    public List<String> tabComplete(String[] args) {
        return ItemRegister.getAllTypes().stream().filter(r -> r.toLowerCase().startsWith(args[0].toLowerCase())).collect(Collectors.toList());
    }
}
